<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="mini" v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegion" placeholder="请选择" clearable
              @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label"
                :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="服务日期" prop="serviceDate">
            <el-date-picker type="month" clearable v-model="queryParams.serviceDate" value-format="yyyy-MM-dd"
              placeholder="请选择服务日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="收款状态" prop="collectionStatus">
            <el-select v-model="queryParams.collectionStatus" placeholder="请选择" clearable
                       @keyup.enter.native="handleQuery">
              <el-option v-for="item in collectionStatus" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['cld:invoice:export']">列表导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="settlementList" border>
      <el-table-column label="服务日期" align="center" prop="serviceDate">
        <template slot-scope="scope">
          {{ parseTime(scope.row.serviceDate, '{y}-{m}-{d}') }}
        </template>
      </el-table-column>
      <el-table-column label="项目区域" align="center" prop="projectRegion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion" />
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" width="250"/>
      <el-table-column label="项目状态" align="center" prop="contractStatus" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.contractStatus == 1" type="success">履约中</el-tag>
          <el-tag v-if="scope.row.contractStatus == 2" type="danger">已到期</el-tag>
          <el-tag v-if="scope.row.contractStatus == 3" type="info">已终止</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="开票状态" align="center" prop="invoiceStatus">
        <template slot-scope="scope">
          <dict-tag :options="invoiceStatusOptions" :value="scope.row.invoiceStatus" />
        </template>
      </el-table-column>
      <el-table-column label="应收金额" align="center" prop="answerInvoiceAmount">
        <template slot-scope="scope">
          {{ toMoney(scope.row.answerInvoiceAmount) }}
        </template>
      </el-table-column>
      <el-table-column label="开票金额" align="center" prop="realInvoiceAmount">
        <template slot-scope="scope">
          {{ toMoney(scope.row.realInvoiceAmount) }}
        </template>
      </el-table-column>
      <el-table-column label="实收金额" align="center" prop="receivedMoney">
        <template slot-scope="scope">
          {{ toMoney(scope.row.receivedMoney) }}
        </template>
      </el-table-column>
      <el-table-column label="坏账" align="center">
        <template slot-scope="scope">
          {{ toMoney(scope.row.baddebtMoney) }}
        </template>
      </el-table-column>
      <el-table-column label="未收款" align="center" prop="projectId">
        <template slot-scope="scope">
          {{ !scope.row.collectionTime ? toMoney(scope.row.realInvoiceAmount - scope.row.receivedMoney) : toMoney(0)}}
        </template>
      </el-table-column>
      <el-table-column label="应结算日期" align="center" prop="settlementDate" />
      <el-table-column label="收款日期" align="center" prop="collectionTime" />
      <el-table-column label="延迟天数" align="center" prop="diffDay" >
        <template slot-scope="scope">
          <span v-if="parseInt(scope.row.diffDay) > 0">{{scope.row.diffDay}}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否烂账" align="center" prop="isMashed" >
        <template slot-scope="scope">
          {{ scope.row.isMashed === 1 ? '是' : '-'}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['cld:invoice:edit']">修改</el-button>
          <el-button size="mini" type="text" v-if="scope.row.collectionStatus == 0" @click="handleConfirmPayment(scope.row)"
            v-hasPermi="['cld:invoice:payment']">收款</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-position="top" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" :disabled="true" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="服务月份" prop="serviceDate">
              <el-input v-model="form.serviceDate" :disabled="true" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收款日期" prop="collectionTime">
              <el-date-picker clearable v-model="form.collectionTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="应收金额" prop="answerInvoiceAmount">
              <el-input v-model="form.answerInvoiceAmount" :disabled="true" placeholder="" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="坏账" prop="baddebtMoney">
              <el-input type="number" v-model="form.baddebtMoney" placeholder="请输入" />
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="实收金额" prop="receivedMoney">
              <el-input type="number" v-model="form.receivedMoney" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否烂账" prop="isMashed">
              <el-radio-group v-model="form.isMashed">
                <el-radio :label="1" border style="margin-right:5px;">是</el-radio>
                <el-radio :label="0" border>否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSettlement, getSettlement, updateSettlement } from "@/api/cld/settlement";
import { invoiceStatusOptions, signStatusOptions, serviceTypeOptions } from '@/assets/staticDict'
export default {
  name: "region",
  dicts: ['cld_project_region'],
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算管理表格数据
      settlementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectRegion: null,
        projectName: null,
        serviceDate: null
      },
      // 开票状态
      invoiceStatusOptions: invoiceStatusOptions,
      // 表单参数
      form: {
        isMashed:0,
      },
      // 表单校验
      rules: {
        collectionTime: [
          { required: true, message: "收款日期不能为空", trigger: "blur" }
        ],
      },
      //是否收款选项
      collectionStatus:[{
        value:'0',
        label:'未收款'
      },{
        value:'1',
        label:'已收款'
      }]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询发票管理列表 */
    getList() {
      this.loading = true;
      listSettlement(this.queryParams).then(response => {
        this.settlementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getSettlement(row.settlementId).then(response => {
        this.form = response.data;
        this.form.serviceDate = this.parseTime(this.form.serviceDate, '{y}-{m}-{d}');
        this.form.receivedMoney = this.form.receivedMoney ? this.form.receivedMoney : this.form.realInvoiceAmount;
        this.open = true;
        this.title = "结算信息";
      });
    },
    // 收款
    handleConfirmPayment(row) {
      this.$modal.confirm('是否确认收款？').then(function () {
        let form = {
          settlementId: row.settlementId,
          collectionStatus: 1,
          collectionTime: new Date(),
          receivedMoney: row.answerInvoiceAmount
        }
        return updateSettlement(form);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => { });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.collectionStatus = 1;
          updateSettlement(this.form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
     /** 导出按钮操作 */
     handleExport() {
      this.download('cld/invoice/export', {
        ...this.queryParams
      }, `结算管理_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
